/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dafe.partyband.db.impl.handlers;

import com.db4o.ObjectContainer;
import org.dafe.partyband.db.api.IBandManager;
import org.dafe.partyband.db.api.ISong;
import org.dafe.partyband.db.api.ISongResource;
import org.dafe.partyband.db.api.handlers.ISongHandler;
import org.dafe.partyband.db.api.handlers.ISongResourceHandler;

/**
 *
 * @author dafe
 */
public class SongHandler implements ISongHandler {

    private ObjectContainer db;
    private final IBandManager manager;
    
    private ISongResourceHandler resHandler;

    public SongHandler(ObjectContainer db, IBandManager manager) {
        this.db = db;
        this.manager = manager;
    }

    public void removeSong(ISong song, boolean removeResources) {
        if (removeResources) {
            for (ISongResource curRes : song.getResources()) {
                if (curRes != null) {
                    getResHandler().delete(curRes);
                }
            }
        }
        db.delete(song);
    }

    public void storeSong(ISong song) {
        for (ISongResource curRes : song.getResources()) {
            getResHandler().store(curRes);
        }
        db.store(song);
    }

    private ISongResourceHandler getResHandler() {
        if (resHandler == null) {
            resHandler = manager.getHandlersLookup().lookup(ISongResourceHandler.class);
        }
        return resHandler;
    }

    public void exportSong(ISong song) {
        for (ISongResource curRes : song.getResources()) {
            getResHandler().exportFromDbToFile(curRes);
        }
    }

    public void addResource(ISong song, ISongResource songRes, boolean importData) {
        if (importData) {
            getResHandler().store(songRes);
        }
        song.getResources().add(songRes);
        db.store(song.getResources());
    }

    public void removeResource(ISong song, ISongResource songRes) {
        song.getResources().remove(songRes);
        getResHandler().delete(songRes);
        db.store(song.getResources());
    }

    public void storeSongName(ISong song) {
        db.store(song);
    }

    public void storeAuthorName(ISong song) {
        db.store(song.getAuthor());
    }

    public void setObjectContainer(ObjectContainer db) {
        this.db = db;
    }
    
}
